<template>
  <el-dialog v-dialog-drag append-to-body :close-on-click-modal="false" width="800px" :visible.sync="visible"
    :before-close="beforeClose" :title="title">
    <div class="wrapper">
      <el-form ref="form" :rules="operateType.indexOf('View') > -1 ? {} : rules" :model="reviewForm" label-width="100px">
        <el-form-item label="复查结果" prop="operatorStatus">
          <el-radio v-model="reviewForm.operatorStatus" label="3"
            :disabled="operateType.indexOf('View') > -1">合格</el-radio>
          <el-radio v-model="reviewForm.operatorStatus" label="2"
            :disabled="operateType.indexOf('View') > -1">不合格</el-radio>
        </el-form-item>
        <el-form-item label="复查说明" prop="operatorExplain">
          <el-input type="textarea" :rows="4" v-model="reviewForm.operatorExplain" placeholder="请输入复查说明"
            :disabled="operateType.indexOf('View') > -1"></el-input>
        </el-form-item>
        <el-form-item label="整改成因" prop="questionOrigin">
          <el-input type="textarea" :rows="4" v-model="questionOrigin" placeholder="请输入成因" disabled></el-input>
        </el-form-item>
      </el-form>
      <div v-for="(item, index) in reviseList" :key="index">
        <el-divider direction="horizontal" content-position="left">整改信息</el-divider>
        <ReviseForm ref="reviewForm" operateType="view" :subpackageUnitIdsOptions="subpackageUnitIdsOptions"
          :form="item" />
        <div style="text-align: center;margin-bottom: 10px;"></div>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" size="mini" @click="submit" v-if="operateType.indexOf('View') == -1">确定</el-button>
      <el-button size="mini" @click="beforeClose">取消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import ReviseForm from "./reviseForm";
import { doReview, getSubUnits, getSelectReviewCheck, getReviewDetail } from "../api";
function required() {
  return { required: true, message: "此项不能为空", trigger: "change" }
}
export default {
  name: "ReviewDialog",
  props: {
    visible: Boolean,
    title: {
      type: String,
      default: "任务复查"
    },
    data: {
      type: Object,
      default: () => { }
    },
    operateType: {
      type: String,
      default: "review"
    },
  },
  components: { ReviseForm },
  data() {
    return {
      projectId: this.$store.state.app.selectData,
      subpackageUnitIdsOptions: [],
      reviseList: [],
      recordId: "",
      questionOrigin: "",
      reviewForm: {
        operatorStatus: "2",
        operatorExplain: "",
      },
      rules: {
        operatorStatus: [required()],
      },
    };
  },
  created() {
    this.initData();
  },
  methods: {
    async initData() {
      this.recordId = this.data.id;
      const res = await getSubUnits();
      this.subpackageUnitIdsOptions = res.rows;
      if (this.operateType == "review") {
        const detailRes = await getSelectReviewCheck(this.data.id); // todo pics处理
        // this.questionOrigin = detailRes.data.bizRectifyReview?.questionOrigin;
        this.questionOrigin = this.data.questionOrigin;
        this.reviseList = detailRes.data.listBizRectifyReview.map(item => ({
          ...item,
          pics: item.operatorPhotos.map(item => process.env.VUE_APP_MINIO_BASE_API+ '/'+ item.bucket + "/" + item.name), // todo 图片处理   fineAttachment
        }))
      } else if (this.operateType == 'reviewView') {
        const detailRes = await getReviewDetail({ id: this.data.id, recordId: this.data.recordId }); // todo pics处理
        this.reviewForm.operatorStatus = detailRes.data.bizRectifyReview.operatorStatus;
        this.reviewForm.operatorExplain = detailRes.data.bizRectifyReview.operatorExplain;
        this.reviseList = detailRes.data.listBizRectifyReview.map(item => ({
          ...item,
          pics: item.operatorPhotos.map(item => process.env.VUE_APP_MINIO_BASE_API+ '/'+ item.bucket + "/" + item.name), // todo 图片处理
        }))
        this.questionOrigin = detailRes.data.questionOrigin
      }
    },
    beforeClose() {
      this.$emit('update:visible', false);
    },
    async submit() {
      if (this.reviewForm.operatorStatus == "2" && !this.reviewForm.operatorExplain.length) {
        return this.$message.warning("复查不合格时，复查说明不能为空")
      }
      this.$refs.form.validate(async valid => {
        if (!valid) {
          return;
        }
        const res = await doReview({
          recordId: this.recordId,
          ...this.reviewForm,
          projectId: this.projectId
        })
        if (res.code == 200) {
          this.$emit("refresh")
          this.beforeClose()
        }
      })
    },
  },
};
</script>
<style lang="scss" scoped>
.wrapper {
  max-height: 500px;
  overflow-y: auto;
  overflow-x: hidden;

  .el-select {
    width: 100%;
  }

  .el-date-editor {
    width: 100%;
  }
}
</style>
